import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/label_list_bean.dart';
import 'package:hbtrans_flutter/main/message/page.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';

import '../../apis.dart';
import '../../const.dart';

class Message extends StatefulWidget {
  @override
  _MessageState createState() => _MessageState();
}

class _MessageState extends State<Message> {
  List<Widget> tabs = List();
  List<Widget> pages = List();
  var loadingDialog;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _labelList();
  }

  @override
  Widget build(BuildContext context) {
    return tabs.length == 0
        ? Container()
        : TabBarWidget(
            initPage: 0,
            type: TabBarWidget.TOP_TAB,
            tabItems: tabs,
            tabViews: pages,
            indicatorColor: Color(0xff23AFDC),
          );
  }

  _labelList() {
    MyDio.getInstance(url: Const.TRANS_SERVER_MY_PATH).get(
      Apis.labelList,
      (data) {
        if (loadingDialog != null) {
          loadingDialog = null;
          Navigator.pop(context);
        }

        setState(() {
          for (var s in data) {
            tabs.add(Container(
                height: 44,
                child: Center(
                  child: Text(
                    LabelListBean.fromJson(s).name,
                    style: TextStyle(fontSize: 16),
                  ),
                )));
            pages.add(Page(labelId: LabelListBean.fromJson(s).id));
          }
        });
      },
    );
  }
}

class TabBarWidget extends StatefulWidget {
  static const int BOTTOM_TAB = 1;
  static const int TOP_TAB = 2;
  final int type;
  final String hostTitle;
  final List<Widget> tabItems;
  final List<Widget> tabViews;
  final Color backgroundColor;
  final Color indicatorColor;
  final Widget title;
  final int initPage;
  final ValueChanged<int> onPageChanged;

  TabBarWidget(
      {Key key,
      this.hostTitle,
      this.type,
      this.tabItems,
      this.tabViews,
      this.backgroundColor,
      this.indicatorColor,
      this.title,
      this.initPage,
      this.onPageChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TabBarState(this.hostTitle, this.type, this.tabViews,
        this.indicatorColor, this.title, this.initPage, this.onPageChanged);
  }
}

class _TabBarState extends State<TabBarWidget>
    with SingleTickerProviderStateMixin {
  final String _hostTitle;
  final int _type;
  final int _initPage;
  final List<Widget> _tabViews;
  final Color _indicatorColor;
  final Widget _title;
  final ValueChanged<int> _onPageChanged;

  _TabBarState(this._hostTitle, this._type, this._tabViews,
      this._indicatorColor, this._title, this._initPage, this._onPageChanged)
      : super();
  TabController _tabController;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
        initialIndex: _initPage, length: widget.tabItems.length, vsync: this);
    _pageController = PageController(initialPage: _initPage, keepPage: true);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 20,
        ),
        TabBar(
          tabs: widget.tabItems,
          controller: _tabController,
          indicatorColor: _indicatorColor,
          indicatorSize: TabBarIndicatorSize.label,
          labelColor: Color(0xff23AFDC),
          unselectedLabelColor: Color(0xff999999),
          onTap: (index) {
            _pageController.jumpToPage(index);
          },
        ),
        Expanded(
          child: PageView(
            controller: _pageController,
            children: _tabViews,
            onPageChanged: (index) {
              _tabController.animateTo(index);
              _onPageChanged?.call(index);
            },
          ),
        ),
      ],
    );
  }
}
